﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using DG.Tweening;
using System.Collections;

public class SnapButton : MonoBehaviour , IPointerClickHandler {

	public Cam c;
	public GameObject SnapPanel;  
	public GameObject DisplayAvatarPanel;
	public GameObject DisplayAvatarFrame;

	GameObject avatarGroup;
	GameObject playerNameTitle;
	GameObject playerName;
	GameObject okButton;

	public void Awake(){
		avatarGroup = GameObject.Find ("DisplayAvatarGroup");
		playerNameTitle = GameObject.Find ("PlayerNameTitle");
		playerName = GameObject.Find ("PlayerName");
		okButton = GameObject.Find ("OKButton");

		okButton.GetComponent<CanvasGroup>().alpha = 0;
		DisplayAvatarPanel.transform.localScale = Vector3.zero;
		DisplayAvatarFrame.transform.localScale = Vector3.zero;
		DisplayAvatarFrame.GetComponent<CanvasGroup>().alpha = 0;
	}

	public void OnPointerClick(PointerEventData data) {

		FlashBang f = GetComponent<FlashBang> ();
		f.MineHit ();

		// 头像截图
		c.CutOutFace ();

		// 移开相机
		SnapPanel.transform.localScale = new Vector3(0,0,0);

		// 移入头像
		DisplayAvatarPanel.transform.localScale = new Vector3(1,1,1);

		// 整体动画
		MoveAvatar ();

	}

	void MoveAvatar() {


		float ScaleRate = 240f / DisplayAvatarPanel.GetComponent<RectTransform> ().rect.width;

		Sequence seq = DOTween.Sequence ();

		seq.AppendInterval (.5f);

		// 头像动画
		seq.Append( DisplayAvatarPanel.GetComponent<RectTransform>().DOScale( new Vector3( ScaleRate , ScaleRate , ScaleRate ) , .2f ) );

		seq.AppendInterval (.2f);

		// 头像框动画
		DisplayAvatarFrame.transform.localScale = new Vector3( 2f , 2f, 2f );
		seq.Append( DisplayAvatarFrame.GetComponent<RectTransform>().DOScale( new Vector3( ScaleRate,ScaleRate,ScaleRate ) , .2f ) );
		seq.Join( DisplayAvatarFrame.GetComponent<CanvasGroup>().DOFade( 1f , .2f) );

		seq.AppendInterval (.2f);

		// 头像与头像框移位
		seq.Append( avatarGroup.GetComponent<RectTransform>().DOMove( new Vector3( -350.0f , 0 , 0 ) , .2f ) );


		// Title移位
		Text pt = playerNameTitle.GetComponent<Text>();
		Color o = pt.color;
		pt.color = new Color (o.r, o.g, o.b, 0);

		seq.AppendInterval (.5f);

		seq.Append (playerNameTitle.GetComponent<RectTransform> ().DOMove (new Vector3 (110, 40, 0), .2f)); 
		seq.Join( playerNameTitle.GetComponent<Text> ().DOColor (new Color (o.r, o.g, o.b, 1), .5f) );

		// Name移位
		seq.Append( playerName.GetComponent<RectTransform>().DOMove( new Vector3( 110 , -60 , 0), .2f));

		// OK按钮
		seq.Append( okButton.GetComponent<CanvasGroup>().DOFade(1,.5f));

	}
}
